package src.backtracking_algorithm;

import java.util.List;

/**
 * @author starsea
 * @date 2024-07-30 22:15
 */

public class test01 {
    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {

    }

    //汉诺塔问题
    public static void hanota(List<Integer> A, List<Integer> B, List<Integer> C) {
        dfs(A.size(), A, B, C);
    }

    private static void dfs(int n, List<Integer> a, List<Integer> b, List<Integer> c) {
        if (n == 1) {
            c.add(a.remove(a.size() - 1));
            return;
        }
        dfs(a.size(), a, c, b);
        c.add(a.remove(a.size() - 1));
        dfs(a.size(), b, a, c);
    }

    //合并两个有序链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        } else if (list2 == null) {
            return list1;
        } else if (list2.val > list1.val) {
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        } else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }

    //反转链表
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }

    //两两交换链表中的节点
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = swapPairs(head.next.next);
        ListNode tmp = head.next;
        tmp.next = head;
        head.next = newHead;
        return tmp;
    }

    //快速幂
    public double myPow(double x, int n) {
        if (n >= 0) {
            return Pow(x, n);
        } else {
            return 1.0 / Pow(x, -n);
        }
    }

    public double Pow(double x, int n) {
        if (n == 0) {
            return 1.0;
        }
        double tmp = Pow(x, n / 2);
        return n % 2 == 0 ? tmp * tmp : tmp * tmp * x;
    }

}
